package com.shop.cereshop.app.controller.sos;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.shop.cereshop.app.service.sos.CereSosPhoneService;
import com.shop.cereshop.commons.domain.sos.CereSosPhone;
import com.shop.cereshop.commons.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * SOS模块
 */
@RestController
@RequestMapping("sos")
@Slf4j(topic = "SosController")
@Api(value = "一键急救", tags = "一键急救")
public class SosController {

    @Autowired
    private CereSosPhoneService cereSosPhoneService;

    @Value("${gaode.key}")
    private String gaodeKey;
    /**
     * 获取附近医院接口
     */
    private static final String NEARBY_URL = "https://restapi.amap.com/v3/place/text?keywords=宠物医院&city=<city>&offset=<每页记录数据>&page=<当前页数>&key=<用户的key>&extensions=all";
    /**
     * 附近医院
     * @return
     */
    @PostMapping("nearby")
    @ApiOperation(value = "☆附近医院",notes="返回值参数查询文档https://lbs.amap.com/api/webservice/guide/api-advanced/search#s10")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "city", value = "城市编码", required = false, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "offset", value = "每页记录数据数量", required = false, dataType = "Integer", paramType = "query"),
        @ApiImplicitParam(name = "page", value = "当前页数", required = false, dataType = "Integer", paramType = "query")
    })

    public Result<JSONObject> nearby(String city,Integer offset,Integer page) {
        String url = NEARBY_URL.replace("<用户的key>", gaodeKey);
        url = url.replace("<city>", city);
        url = url.replace("<每页记录数据>", offset.toString());
        url = url.replace("<当前页数>", page.toString());
        log.info("url:{}",url);
        //发送请求
        JSONObject jsonObject = JSONObject.parseObject(HttpUtil.get(url));
        return Result.success(jsonObject);
    }

    /**
     * 根据地区编码获取急救电话
     */
    @PostMapping("phone")
    @ApiOperation(value = "☆急救电话", notes = "根据地区编码获取配置的一键急救电话")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cityCode", value = "地区编码", required = true, dataType = "String", paramType = "query")
    })
    public Result<CereSosPhone> phone(String cityCode) {
        if (StringUtils.isBlank(cityCode)) {
            return Result.success(null);
        }
        CereSosPhone phone = cereSosPhoneService.getByCityCode(cityCode);
        return Result.success(phone);
    }
}
